#include <iostream>
#include <nanomsg/nn.h>
#include <nanomsg/pipeline.h>
#include <nanomsg/pubsub.h>
#include <nanomsg/tcp.h>
#include "messages.pb.h"

int main() {
    int socket = nn_socket(AF_SP, NN_TCP_NODELAY);
    nn_bind(socket, "tcp://127.0.0.1:5555");

    while (true) {
        char* buffer = nullptr;
        int bytes = nn_recv(socket, &buffer, NN_MSG, 0);

        Request request;
        request.ParseFromArray(buffer, bytes);

        std::cout << "Received request: " << request.data() << std::endl;

        Response response;
        response.set_result("Hello, " + request.data());

        std::string serialized_response;
        response.SerializeToString(&serialized_response);

        nn_send(socket, serialized_response.c_str(), serialized_response.size(), 0);

        nn_freemsg(buffer);
    }

    nn_shutdown(socket, 0);
    return 0;
}
